#![allow(clippy::needless_doctest_main)]

use crate::config::Config;
use crate::serializer::serialization::{DeSerialized, Serialized};
use crate::start::server::{Service, ServiceBuilder};
use crate::start::{Connection, SendMsg, SendPacket};
use bytes::{Bytes, BytesMut};
use tokio::net::TcpListener;
use tokio::runtime::Runtime;

pub mod codec;
pub mod config;
pub mod serializer;
pub mod start;
pub mod p2p;

#[tokio::test]
async fn test() {
    fn t(con: Connection) {
        let packet = con.get_packet();
        println!("{:?}", packet);
        let mut bytes =MyBytes(Bytes::new());
        let x = &mut bytes;
        con.de_serialized(x);

        println!("{:?}", bytes);
        con.send_packet(SendPacket::default());
        con.send_msg::<MyBytes>(SendMsg::from(packet));
        println!("test dispatchers 2 ok");
    }

    #[derive(Debug)]
    pub struct MyBytes(Bytes);

    impl Serialized for MyBytes {
        fn serialized(&self) -> Bytes {
            Bytes::new()
        }
    }

    impl DeSerialized for MyBytes {
        // type Obj = Bytes;

        fn deserialized(&mut self, body: &Bytes) -> &mut Self {
            // Bytes::from(body.to_vec());
            self.0=Bytes::from(body.to_vec());
            self
        }
    }

    ServiceBuilder::default(Config::debug_default_tcp())
        .register(1, |con| println!("test dispatchers 1 ok"))
        .register(2, t)
        .builder()
        .start()
        .await;
}
